package com.example.schoolexam2.service.impl;

import com.example.schoolexam2.dto.EmployeeDto;
import com.example.schoolexam2.entity.Employee;
import com.example.schoolexam2.entity.EmployeeTemporaryBean;
import com.example.schoolexam2.mapper.EmplMapper;
import com.example.schoolexam2.service.EmployeeService;
import com.example.schoolexam2.utils.JwtUtils;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import static com.example.schoolexam2.utils.InformationToolClass.Employeeconversion;


@Service
public class EmployeeImpl implements EmployeeService {
    @Autowired
    EmplMapper emplMapper;

    @Override
    public int employeeIncreases(MultipartFile file, int RequestType)throws Exception {
        Workbook workbook;
        try {
            if(RequestType==0){
                workbook = new HSSFWorkbook(file.getInputStream());
            }else if (RequestType==1){
                workbook = new XSSFWorkbook(file.getInputStream());
            }else {
                return 0;
            }
            Sheet sheetAt = workbook.getSheetAt(0);
            int lastRowNum = sheetAt.getLastRowNum();
            Vector<EmployeeTemporaryBean> vector = new Vector<>();
            for (int i = 3; i <= lastRowNum; i++) {
                EmployeeTemporaryBean employeeTemporaryBean = new EmployeeTemporaryBean();
                Row row = sheetAt.getRow(i);
                if (row != null) {
                    employeeTemporaryBean = Employeeconversion(row);
                }
                vector.add(employeeTemporaryBean);
            }

            int res = 0;
            for (int i = 0; i < vector.size(); i++) {
                res = res + emplMapper.employeeIncreases(vector.get(i));
            }
            return res;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public Employee employeePersonal(String employeeId){

        try{
            Employee employee = emplMapper.employeePersonal(employeeId);
            if(employee.getIsUser()==0){
                return employee;
            }else{
                return null;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public int employeeDelete(String employeeId, int isUser){
        int rse=0;
        try {
            Employee employee = emplMapper.employeePersonal(employeeId);
            if (employee==null){
                return 0;
            }else if(employee.getIsUser()==1){
                rse= emplMapper.employeeDeleteTrue(employeeId);
            }else{
                rse = emplMapper.employeeDelete(employeeId, isUser);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return rse;
    }

    @Override
    public int employeeIncrease(EmployeeDto employeeDto){
        int res = 0;
        try{
            if(employeeDto.getPassword().isEmpty()){
                return 0;
            }else {
                String password=employeeDto.getPassword();
                String newpassword= JwtUtils.addM(password);
                employeeDto.setPassword(newpassword);
            }
             res=emplMapper.employeeIncrease(employeeDto);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return res;
    }

    @Override
    public int employeeUpdate(EmployeeDto dto) {
        int res=0;
        try {
            res = emplMapper.employeeUpdate(dto);
        }catch(Exception e){
            e.printStackTrace();
        }
        return res;
    }

    @Override
    public List<Employee> employeeFuzzy(String Fuzzy) {
        Employee employee;
        List<Employee> listAll = new ArrayList();
        try{
            List<Employee> list = emplMapper.employeeFuzzy(Fuzzy);
            if (list.size()==0){
                return null;
            }else {
                for (int i = 0; i < list.size(); i++) {
                    employee = list.get(i);
                    if (employee.getIsUser() == 0) {
                        listAll.add(employee);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return listAll;
    }

}
